package com.zc.app.sys.controller;

import com.zc.framework.system.BaseController;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.ShiroException;
import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by zc on 2017/1/5.
 */
@RequestMapping(value = "/error")
@ControllerAdvice
public class ErrorController extends BaseController{

    private static Logger logger = LogManager.getLogger(ErrorController.class);

    @ExceptionHandler(value = {UnauthenticatedException.class})
    public RedirectView unauthenticated(WebRequest request, HttpServletRequest request2) {
        HttpServletRequest http_request = (HttpServletRequest) request2;
        String path = http_request.getContextPath();
        return new RedirectView(String.format("%s/unauthorized", path));
    }

    @RequestMapping(value = "/unauthenticated")
    public RedirectView unauthenticated(RedirectAttributesModelMap redirectAttributesModelMap,
                                        HttpServletRequest request) {
        HttpServletRequest http_request = (HttpServletRequest) request;
        String path = http_request.getContextPath();
        return new RedirectView(String.format("%s/login", path));
    }

    @ExceptionHandler(value = {ShiroException.class})
    @RequestMapping(value = "/unauthorized")
    public ModelAndView unauthorized() {
        return new ModelAndView("unauthorized");
    }

//    @ExceptionHandler(value = {PageAclException.class})
//    @RequestMapping(value = "/pageAclRequired")
//    public ModelAndView pageAclRequired() {
//        return new ModelAndView("error/pageAclRequired");
//    }
}
